-- Decompiled with the Synapse X Luau decompiler.

local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerRankComponent());
local RankList = ServerOHOH.load.RankList();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();

function M.new(self)
	return M(self);
end;
local Server_RankManager = ServerOHOH.load.Server_RankManager();
function M.ctor(self, parent)

	M.super.ctor(self, parent);

	for k,v in pairs(RankList) do
		local s, value = Server_RankManager:GetRankValue(k, self.parent:GetUserId());
		if not s then
			self[k] = 0;
			continue;
		end;
		self[k] = value or 0;
	end
	self.DataBase= self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());
	assert(self.DataBase,"self.DataBase==null")
	self.StartTime = self.DataBase:GetData("startTime");
	
	self:AddProcess(function()
		self:Wait(10)
		self:Update()
		self:AddUpdate(self.Update, 60);
	end);

end;

function M.UpdateRankBase(self)
	if not self.oldPower then
		self.oldPower=self.DataBase:GetData("base").power
		self.oldRebirth=self.DataBase:GetData("base").rebirth
		self.oldWins=self.DataBase:GetData("base").wins
	end

	local nowPower=self.DataBase:GetData("base").power
	local nowRebirth=self.DataBase:GetData("base").rebirth
	local nowWins=self.DataBase:GetData("base").wins
	if self.oldPower~= nowPower then
		self:SaveRankBaseData("power",GeneralTool.ShortRankNumber(nowPower))
		self.oldPower=nowPower
	elseif self.oldRebirth~= nowRebirth then
		self:SaveRankBaseData("rebirth",nowRebirth)
		self.nowRebirth=nowRebirth
	elseif self.oldWins~= nowWins then
		self:SaveRankBaseData("wins",GeneralTool.ShortRankNumber(nowWins))
		self.oldWins=nowWins
	end
end
function M.dtor(self)
	M.super.dtor(self);
end;

local DefaultPlayerData = ServerOHOH.load.PlayerData();

function M.SaveRankBaseData(self,key, value)
	if DefaultPlayerData.isTest then
		return
	end
	value = math.ceil(value);
	
	self:SetValue(key, function()
		return value;
	end);
	-- self[key]=value

end;

function M.SaveRankIncrementData(self,key, value)
	if DefaultPlayerData.isTest then
		return
	end
	
	if value  then
		self:Increment(key,value)
		-- self[key]=self[key]+value
	end
end;

function M.Update(self)
	self:UpdateRankBase()
	self:SaveRankBaseData("time",os.time()-self.StartTime)
end;


return M;
